#!/usr/bin/env node


const http = require('http'),
      fs = require('fs'),
      log = console.log,
      qs = require('querystring');
function err(req,res){
  res.statusCode = 404;
  res.end(`${req.url} is not found`);
}

http.createServer((req,res) => {
   /*log(`响应起始行：HTTP/${req.httpVersion} ${req.statusCode} ${req.statusMessage}`)*/
   //log(req.headers);
   /*log();*/
  if(req.url === '/'){
    if(req.method === 'GET'){
      const html = fs.readFileSync('./04-form.html').toString('utf8');
      res.end(html);
    }else if(req.method === 'POST'){
      let data = '';
      req.on('data',chunk => {
        data += chunk;
      })
      req.on('end',() => {
        qs.parse(data);
        res.end('ok');
      })
    }else{
      err(req,res);
    }
  }else{
    err(req,res);
  }
}).listen(3000);
